package com.devilloki.viki.overspeedalert.tools;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.preference.PreferenceManager;

/** 工具类,用于参数配置
 *	<br>
 * 同时也用于临时存放经纬度的数据
 * @author viki
 *
 */

public class SettingsHelper {

    /**
     * 本程序自用的<Br>SharedPreferences
     */
    private SharedPreferences mPreferences = null;

    /**
     * 使用app的context生成的默认的SharedPreferences
     */

    public SettingsHelper(Context context) {
        context.getApplicationContext();
        this.mPreferences = PreferenceManager.
                getDefaultSharedPreferences(context.getApplicationContext());
    }

    public String getString(String key, String defaultValue) {
        if (mPreferences != null) {
            return mPreferences.getString(key, defaultValue);
        }
        return defaultValue;
    }

    public int getInt(String key, int defaultValue) {
        if (mPreferences != null) {
            return mPreferences.getInt(key, defaultValue);
        }

        return defaultValue;
    }

    public boolean getBoolean(String key, boolean defaultValue) {
        if (mPreferences != null) {
            return mPreferences.getBoolean(key, defaultValue);
        }
        return defaultValue;
    }

    public void setString(String key, String value) {
        Editor editor = null;
        if (mPreferences != null) {
            editor = mPreferences.edit();
        }

        if (editor != null) {
            editor.putString(key, value);
            editor.commit();
        }
    }

    public void setBoolean(String key, boolean value) {
        Editor editor = null;
        if (mPreferences != null) {
            editor = mPreferences.edit();
        }

        if (editor != null) {
            editor.putBoolean(key, value);
            editor.commit();
        }
    }

    public void setInt(String key, int value) {
        Editor editor = null;
        if (mPreferences != null) {
            editor = mPreferences.edit();
        }

        if (editor != null) {
            editor.putInt(key, value);
            editor.commit();
        }
    }


}
